<?php

/**
 * QuestionController - The controller for Games class
 *
 * @author
 * @version
 */

require_once 'Zend/Controller/Action.php';

class Adminb_QuestionController extends Zend_Controller_Action
{

    public function init()
    {

        $member=new CMember();

        if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName()))
        {


            $this->_redirect(MY_URL.'/auth-login?preurl='.CURRENT_URL);


        }

    }

    public function preDispatch()
    {
        $this->getHelper ('layout')->setLayout('framelayout');

        $this->view->addHelperPath(MY_ROOT.'/vhelpers');

        Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

    }


    public function indexAction()
    {
        $page=$this->_request->getParam("page",1);

        $subjectArray = new CQuestionsubject();

        $counts = $subjectArray->getAllsubject();

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage ( $page, $counts, 10 );
        $itemArray = $subjectArray->getsubject($pageArray['from'],10);

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/adminb-question-index?"); //生成分页字符串

        $this->view->assign ( "pageString", $pageString );

        $this->view->assign ( "pageArray", $pageArray );

        $this->view->assign ( 'itemArray', $itemArray );
    }

    public function mybetcAction()
    {
        $page=$this->_request->getParam("page",1);

        $fbbet=new CQuestionsubet();

        $counts = $fbbet->getAllBetCounts();

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage ( $page, $counts, 10 );
        $itemArray = $fbbet->getAllBets($pageArray['from'],10);

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/adminb-question-mybetc?"); //生成分页字符串

        $this->view->assign ( "pageString", $pageString );

        $this->view->assign ( "pageArray", $pageArray );

        $this->view->assign ( 'itemArray', $itemArray );
        
    }
    public function betcAction()
    {

        $id=$this->_request->getParam('id');

        $subject=new CQuestionsubject();

        $editArray=$subject->getsubjectdetail($id);

        $this->view->assign ( "editArray", $editArray );
    }

    public function dobetcAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        if(!$this->getRequest()->isPost())
        {

            $this->view->showMsg("发生未知错误!");

            return false;
        }

        $paramArray=$this->_request->getParams();

        $member=new CMember();

        $integral =$member->getintegral($_COOKIE[COOKIE_PRE.'uid']);
        if($integral[0]['integral'] == 0 || $integral[0]['integral'] < $paramArray['chips'])
        {

            $this->view->showMsg("您的积分不足!");

            return false;
        }else
        {
            $this->view->assign('integral',$integral[0]['integral']);
        }
        $filters = array(
                '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);

        //
        $fbbet=new CQuestionsubet();
        $data=array(
                'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
                'chips'=>$input->chips,
                'answers'=>$input->answers,
                'sceneid'=>$input->sceneid,
                'add_time'=>time(),
        );
        if($fbbet->createFbbet($data))
        {
            //如果下注成功，则扣除对应会员积分
            $member->updateIntegral($input->chips,$_COOKIE[COOKIE_PRE.'uid']);
        }
        /*插入数据到下注表 end*/

        $this->view->showMsg("您已成功下注，为避免透支，系统已暂时扣除您$input->chips积分!",'adminb-question-index');

        return true;
    }

}
